import { router } from "@kit.ArkUI";
import { _appInfo, ActionState, getErrorInfo, _appError, Option } from "CoffeeSdk";


const FileName = "RouterUtil"

export interface BaseRouterParams {}

export function getRouterParams<T>(): T {
  return router.getParams() as T
}

/**
 * 通过自定义函数规范化页面跳转的参数传递
 *
 * @param url
 * @param params
 */
export function pushRouterWithUrl(url: string, params?: BaseRouterParams) {
  const FunName = "pushRouterWithUrl"
  _appInfo(FileName, FunName, "", ActionState.Start, `push to page,url:${url}`)
  try {
    router.pushUrl({ url: url, params: params })
      .catch((e) => {
        _appInfo(FileName, FunName, "", ActionState.Fail, getErrorInfo(e));
      })
  } catch (e) {
    _appError(FileName, FunName, "", ActionState.Fail, getErrorInfo(e));
  }
  _appInfo(FileName, FunName, "", ActionState.Fin);
}

export function pushRouterWithName(name: string, params?: BaseRouterParams) {
  pushNamedRouter(name, params);
}

/**
 * 此方法用于push一个模块中的page.
 * @param name
 * @param params
 */
export function pushNamedRouter(name: string, params?: BaseRouterParams) {
  const FunName = "pushNamedRouter"
  let lastError: Option<Error> = undefined;
  _appInfo(FileName, FunName, "", ActionState.Start, `router name:${name}`);
  router.pushNamedRoute({ name: name, params: params })
    .then(() => {
      _appInfo(FileName, FunName, "", ActionState.Fin);
    })
    .catch((e) => {
      lastError = e;
      _appError(FileName, FunName, "", ActionState.Fail, getErrorInfo(e));
    })

}



